package com.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.admin.controller.BaseController.Status;
import com.admin.model.Roles;
import com.admin.model.Sys_Function;
import com.admin.model.Sys_User;
import com.admin.util.Constants;
import com.admin.util.MD5;
import com.admin.util.Page;
import com.admin.util.UtilTools;

public class RolesController extends BaseController {
	public static String TOROLE = "/role";
	// 首页列表
	public void index(){
		// 查询条件
		String name = getPara("name");
		String rName = null;
		if(UtilTools.isNotNull(name)){
			rName = "name like '%"+ name +"%'";
		}
		// 处理分页参数
		Page<Roles> page = new Page<Roles>(getPara("pageCurrent"),getPara("pageSize"));
		page.setOrderField(getPara("orderField"));
		page.setOrderDirection(getPara("orderDirection"));
		Roles.dao.listByPage(new String[]{rName},page);
		setAttr("page",page);
		render("/jsp/role/list.jsp");
	}
	
	// 跳转设置权限页面
	public void privilege(){
		Roles roles = Roles.dao.getObject(getParaToLong("id"));
		setAttr("roles",roles);
		render("/jsp/role/setPrivilege.jsp");
	}
	
	// 设置权限
	public void setPrivilege(){
		try {
			Long roleId = getParaToLong("id");
			String funIds = getPara("privilegeIds");
			boolean result = Roles.dao.batchAdd(roleId, funIds);
			if(result){
				ajax(Status.success,TOROLE,true);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajax(Status.error,null,null);
			
		}
	}
	
	// 编辑
	public void detail(){
		Roles roles = Roles.dao.getObject(getParaToLong("id"));
		setAttr("roles",roles);
		render("/jsp/role/add.jsp");
	}
	
	// 保存信息
	public void save(){
		try {
			Roles roles = getModel(Roles.class);
			if (null == roles.get("id")) {
				roles.set("create_time",new Date());
				roles.save();
			} else {
				roles.update();
			}
			ajax(Status.success,TOROLE,true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage(), e);
			ajax(Status.error,null,null);
		}
	}
	
	// 删除
	public void del(){
		try {
			String[] ids = getParaValues("ids");
			boolean result = Roles.dao.del(ids);
			if(result){
				ajax(Status.success,null,false);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(ajax(Status.error,"删除失败"));
		}
	}
	
	/**
	 * 异步请求数据
	 */
	@SuppressWarnings("unchecked")
	public void treeData() {
		try {
			List<Roles> list = new ArrayList<Roles>();
			JSONObject parentJson = new JSONObject();
			parentJson.put("id", "0");
			parentJson.put("pid", "-1");
			parentJson.put("name", "所有角色");
			parentJson.put("open", true);
			list = Roles.dao.query(null);
			JSONArray array = new JSONArray();
			for (Roles roles : list) {
				JSONObject json = new JSONObject();
				json.put("id", roles.get("id"));
				json.put("pid","0");
				json.put("name", roles.get("name"));
				json.put("open", true);
				array.add(json);
			}
			parentJson.put("children", array);
			renderJson(parentJson);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage(), e);
		}
	}
}
